package com.hqhsc.module.manage.channel.service;

import com.hqhsc.core.cache.CacheKey;
import com.hqhsc.module.common.service.CommonService;
import com.hqhsc.module.common.service.ManageMemcacdedClient;
import com.hqhsc.module.log.service.LogService;
import com.hqhsc.module.manage.activityuser.dao.ActivityUserManageDao;
import com.hqhsc.module.manage.channel.dao.ChannelManageDao;
import com.hqhsc.module.manage.channel.domain.Channel;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.util.*;

/**
 *
 * @author jiangyin
 */
@Service
public class ChannelManageService {

	static Logger logger = LoggerFactory.getLogger(ChannelManageService.class);

	@Autowired
	private ChannelManageDao channelManageDao;
    @Autowired
    private CommonService commonService;
    @Autowired
    private LogService logService;
    @Autowired
    private ManageMemcacdedClient memcachedClient;

    public Map<String , Object> getChannelList(long org_id , String channel_name , int channel_type , String orderby_request , int start , int size) {

        String orderby = null;
        String orderby_type = null;
        if(StringUtils.isNotEmpty(orderby_request)) {
            orderby = orderby_request.split(",")[0];
            orderby_type = orderby_request.split(",")[1];
        }

        long total = this.channelManageDao.getChannelCount(org_id , channel_name , channel_type);
        List<Channel> channel_list = this.channelManageDao.getChannelList(org_id , channel_name , channel_type , orderby , orderby_type , start , size);
        DecimalFormat df = new DecimalFormat("0.00");//格式化小数
        for(Channel channel : channel_list) {
            List<String> activity_name_list = this.channelManageDao.getActivityNameByChannelId(channel.getId());
            String activity_name = "";
            for(int i = 0 ; i < activity_name_list.size() ; i++) {
                if("".equals(activity_name)) {
                    activity_name = activity_name_list.get(i);
                } else {
                    activity_name = activity_name + "，" + activity_name_list.get(i);
                }
            }
            channel.setActivity_names(activity_name);

            //计算人均点击成本
            if(channel.getPoint_count() > 0) {
                double avg = Double.valueOf(channel.getSpread_cost())/channel.getPoint_count();
                channel.setPoint_cost_avg(Double.valueOf(df.format(avg)));
            } else {
                channel.setPoint_cost_avg(Double.valueOf(channel.getSpread_cost()));
            }

            //计算人均报名成本
            if(channel.getSignup_count() > 0) {
                double avg = Double.valueOf(channel.getSpread_cost()) / channel.getSignup_count();
                channel.setSignup_cost_avg(Double.valueOf(df.format(avg)));
            } else {
                channel.setSignup_cost_avg(Double.valueOf(channel.getSpread_cost()));
            }
        }

        Map<String , Object> map = new HashMap<String, Object>();
        map.put("total" , total);
        map.put("channel_list" , channel_list);
        return map;
    }

    public int channelDoAdd(long org_id , String channel_name , int channel_type,String website_url, String spread_position, String spread_cycle, String spread_content, int spread_cost , long user_id) {
        int result = this.channelManageDao.insertChannel( channel_name , channel_type,website_url,spread_position,spread_cycle,spread_content,spread_cost , org_id , user_id);
        if(result > 0) {
            logService.insertLog(user_id , "新增渠道" , channel_name+channel_type);
        }
        return result;
    }

    /**
     * 根据id，获取渠道信息
     * 应该还要根据org_id做条件，但是能够修改渠道的人，肯定是该组织下的人。所以暂时不传org_id，后面有需要，再传
     * @param channel_id
     * @return
     */
    public Channel getChannelById(long channel_id) {
        Channel channel = this.commonService.getSysChannelById(channel_id);

        List<String> activity_name_list = this.channelManageDao.getActivityNameByChannelId(channel_id);
        String activity_name = "";
        for(int i = 0 ; i < activity_name_list.size() ; i++) {
            if("".equals(activity_name)) {
                activity_name = activity_name_list.get(i);
            } else {
                activity_name = activity_name + "，" + activity_name_list.get(i);
            }
        }
        channel.setActivity_names(activity_name);
        return channel;
    }

    public int channelDoEdit(long channel_id , long org_id , String channel_name , int channel_type, String spread_position, String spread_cycle, String spread_content, int spread_cost , long user_id ) {
        int result = this.channelManageDao.updateChannel(channel_id , channel_name , channel_type, spread_position, spread_cycle, spread_content, spread_cost , org_id );
        if(result > 0) {
            String key = CacheKey.getChannelKey(channel_id);
            this.memcachedClient.delete(key);
            logService.insertLog(user_id , "更新渠道"+channel_id , "");
        }
        return result;
    }

    public List<Channel> getChannelListByIds(String channel_ids) {
        List<Channel> channel_list = new ArrayList<Channel>();
        String[] channel_id_args = channel_ids.split(",");
        for(int i = 0 ; i < channel_id_args.length ; i++) {
            int channel_id = Integer.valueOf(channel_id_args[i]);
            Channel channel = this.commonService.getSysChannelById(channel_id);

            List<String> activity_name_list = this.channelManageDao.getActivityNameByChannelId(channel_id);
            String activity_name = "";
            for(int j = 0 ; j < activity_name_list.size() ; j++) {
                if("".equals(activity_name)) {
                    activity_name = activity_name_list.get(j);
                } else {
                    activity_name = activity_name + "，" + activity_name_list.get(j);
                }
            }
            channel.setActivity_names(activity_name);

            channel_list.add(channel);
        }
        return channel_list;
    }
}
